package net.yuxianghe.chromepool.util;

import net.yuxianghe.chromepool.Test;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Created by hxy on 2017/5/25.
 */
public class Util {

    private static Log log = LogFactory.getLog(Test.class);


    /**
     * 等待
     *
     * @param driver
     * @param by
     * @param second 秒
     * @return
     */
    public static WebElement webDriverWaitWebElement(WebDriver driver, By by, int second) {
        WebElement element = null;
        try {
            WebDriverWait wait = new WebDriverWait(driver, second);
            element = wait.until((ExpectedCondition<WebElement>) d -> {
                if (d != null) {
                    return d.findElement(by);
                } else {
                    return null;
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage());
            return null;
        }
        return element;
    }
}
